#include "softap.h"

//定义在main文件中
// #define EXAMPLE_ESP_AP_SSID      "BRISK"
// #define EXAMPLE_ESP_AP_PASS      "123456789"
static const char *TAG = "wifi_soft_ap";

static void wifi_event_handler(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data) //wifiap事件处理
{
    if (event_id == WIFI_EVENT_AP_STACONNECTED)
    { //wifiap已连接
        wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
        ESP_LOGI(TAG, "station " MACSTR " join, AID=%d",
                 MAC2STR(event->mac), event->aid);
    }
    else if (event_id == WIFI_EVENT_AP_STADISCONNECTED)
    { //wifiap断开连接
        wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
        ESP_LOGI(TAG, "station " MACSTR " leave, AID=%d",
                 MAC2STR(event->mac), event->aid);
    }
}

esp_err_t wifi_softap_init(void) //softap初始化
{
    ESP_ERROR_CHECK(esp_netif_init()); //在lwip中每一个网络接口都由一个netif结构体表示
    esp_netif_create_default_wifi_ap();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); //AP配置为默认值
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));                //wifi模块初始化

    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); //wifi事件处理

    wifi_config_t wifi_config = {
        //设置wifi参数
        .ap = {
            // .ssid = "EXAMPLE_ESP_AP_SSID",
            // .ssid_len = strlen(EXAMPLE_ESP_AP_SSID),
            .channel = 1,
            // .password = EXAMPLE_ESP_AP_PASS,
            .max_connection = 2,
            .authmode = WIFI_AUTH_WPA_WPA2_PSK},
    };
    extern volatile unsigned int ID_first_four; //MAC初4位做识别
    extern volatile unsigned int ID_mid_four;   //MAC中间4位做识别
    extern volatile unsigned int ID_last_four;  //MAC最后4位做识别

    char ap_ssid[15] = "XW_12345678";
    char ap_password[15] = "123456789101";

    char temp[10];
    itoa(ID_mid_four, temp, 16);
    ap_ssid[4] = temp[0];
    ap_ssid[5] = temp[1];
    ap_ssid[6] = temp[2];
    ap_ssid[7] = temp[3];

    itoa(ID_last_four, temp, 16);
    ap_ssid[8] = temp[0];
    ap_ssid[9] = temp[1];
    ap_ssid[10] = temp[2];
    ap_ssid[11] = temp[3];

    itoa(ID_first_four, temp, 16);
    ap_password[0] = temp[0];
    ap_password[1] = temp[1];
    ap_password[2] = temp[2];
    ap_password[3] = temp[3];

    itoa(ID_mid_four, temp, 16);
    ap_password[4] = temp[0];
    ap_password[5] = temp[1];
    ap_password[6] = temp[2];
    ap_password[7] = temp[3];

    itoa(ID_last_four, temp, 16);
    ap_password[8] = temp[0];
    ap_password[9] = temp[1];
    ap_password[10] = temp[2];
    ap_password[11] = temp[3];

    strcpy((char *)(wifi_config.ap.ssid), ap_ssid);
    strcpy((char *)(wifi_config.ap.password), ap_password);
    wifi_config.ap.ssid_len = strlen(ap_ssid);

    ESP_LOGI(TAG,"softap ssid is %s",ap_ssid);
    ESP_LOGI(TAG,"softap password is %s",ap_password);

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));                //设置wifi模块运行模式
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); //导入wifi参数配置
    ESP_ERROR_CHECK(esp_wifi_stop());                                   //关闭wifi 重复启动时会出错
    ESP_ERROR_CHECK(esp_wifi_start());                                  //wifi启动
    ESP_LOGI(TAG, "AP Station Started...");
    return ESP_OK;
}